package mx.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import mx.entity.Employee;
import mx.entity.PageBean;
import mx.entity.TicketOrder;
import mx.service.TicketOrderService;
import mx.utils.DateJsonValueProcessor;
import mx.utils.ResponseUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


/**
 * 票订单controller
 * 
 * @author root
 *
 */
@Controller
@RequestMapping("/ticketOrder")
public class TicketOrderController {

	@Resource
	private TicketOrderService ticketOrderService;

	@RequestMapping("/list")
	public String list(@RequestParam(value = "page", required = false) String page,TicketOrder ticketOrder,
			@RequestParam(value = "rows", required = false) String rows, HttpServletResponse response)
			throws Exception {
		PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		map.put("status", ticketOrder.getStatus());
		List<TicketOrder> ticketOrderList = ticketOrderService.list(map);
		long totatl = ticketOrderService.getTotal(map);
		JSONObject result = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(ticketOrderList, jsonConfig);
		result.put("rows", jsonArray);
		result.put("total", totatl);
		ResponseUtil.write(response, result);
		return null;
	}
	@RequestMapping("/listById/{id}")
	public String list(@PathVariable("id") Integer id)throws Exception {
		ModelAndView mv = new ModelAndView();
		Map<String, Object> map = new HashMap<String, Object>();
		List<TicketOrder> ticketOrderList = ticketOrderService.list(map);
		long totatl = ticketOrderService.getTotal(map);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(ticketOrderList, jsonConfig);
		return null;
	}

	/**
	 * 添加或者修改票的订单
	 * 
	 * @return
	 */
	@RequestMapping("/save")
	public String save(TicketOrder ticketOrder, HttpServletResponse response) throws Exception {
		int resultTotal = 0;
		JSONObject result = new JSONObject();
		if (ticketOrder.getId() == null) {

		} else {
			resultTotal = ticketOrderService.update(ticketOrder);// 更新订单
		}
		if (resultTotal > 0) {
			result.put("success", true);
		} else {
			result.put("success", false);
		}
		ResponseUtil.write(response, result);
		return null;
	}

}
